import { gatewayList, gatewayEditPars,gatewayEditEnable } from '@/services/api';


export default {
  namespace: 'gateway',

  state: {
    list: [],
    lastResult: {},
  },

  effects: {
    * fetch(_, { call, put }) {
      const response = yield call(gatewayList);
      yield put({
        type: 'saveList',
        payload: response,
      });
    },
    * editPars({ entryId, parameters, success }, { call, put }) {
      const response = yield call(gatewayEditPars, {
        entryId: entryId,
        json: JSON.stringify(parameters),
      });
      yield put({
        type: 'saveLastResult',
        payload: response,
      });
      if(success) success.call();
    },
    *editEnable({entryId, enable,success}, { call, put }){
      const response = yield call(gatewayEditEnable, {
        entryId: entryId,
        enable:enable,
      });
      yield put({
        type: 'saveLastResult',
        payload: response,
      });
      if(success) success.call();
    },
  },

  reducers: {
    saveList(state, action) {
      return {
        ...state,
        list: action.payload.Result,
      };
    },
    saveLastResult(state, action) {
      return {
        ...state,
        lastResult: action.payload,
      };
    },
  },
};
